<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px"
      @submit.native.prevent>
      <el-form-item label="班次名称" prop="classesName">
        <el-input v-model="queryParams.classesName" placeholder="请输入班次名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['cld:classes:add']">新增班次</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="classesList" border>
      <el-table-column label="班次名称" align="center" prop="classesName" />
      <el-table-column label="第一次上下班时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.goToWorkTime1 }} - {{ scope.row.goOffWorkTime1 }}
        </template>
      </el-table-column>
      <el-table-column label="第二次上下班时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.goToWorkTime2 }} - {{ scope.row.goOffWorkTime2 }}
        </template>
      </el-table-column>
      <el-table-column label="第三次上下班时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.goToWorkTime3 }} - {{ scope.row.goOffWorkTime3 }}
        </template>
      </el-table-column>
      <el-table-column label="是否特殊班次(满足其中一次上下班打卡)" align="center" prop="isSpecial">
        <template slot-scope="scope">
          {{ scope.row.isSpecial === 1 ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['cld:classes:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['cld:classes:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改班次管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" size="mini" label-position="top" label-width="80px"
        :inline="true">
        <el-row :gutter="20">
          <el-col :span="16">
            <el-form-item label="班次名称" prop="classesName">
              <el-input v-model="form.classesName" placeholder="请输入班次名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否特殊班" prop="isSpecial">
              <el-radio-group v-model="form.isSpecial">
                <el-radio :label="0">否</el-radio>
                <el-radio :label="1">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="16">
            <el-form-item label="第一次上下班时间" prop="goToWorkTime1">
              <el-time-select placeholder="起始时间" format='HH:mm' value-format='HH:mm' v-model="form.goToWorkTime1"
                v-if="open" :picker-options="{
                  start: '00:10',
                  step: '00:10',
                  end: '23:50'
                }">
              </el-time-select>
              <el-time-select placeholder="结束时间" format='HH:mm' value-format='HH:mm' v-model="form.goOffWorkTime1"
                v-if="open" :picker-options="{
                  start: '00:10',
                  step: '00:10',
                  end: '23:50',
                  minTime: form.isNextTime1 == 1 ? '00:00' : form.goToWorkTime1
                }">
              </el-time-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="次日打卡" prop="isNextTime1">
              <el-radio-group v-model="form.isNextTime1">
                <el-radio :label="0">否</el-radio>
                <el-radio :label="1">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="16">
            <el-form-item label="第二次上下班时间" prop="goToWorkTime2">
              <el-time-select placeholder="起始时间" format='HH:mm' value-format='HH:mm' v-model="form.goToWorkTime2"
                v-if="open" :picker-options="{
                  start: '00:10',
                  step: '00:10',
                  end: '23:50'
                }">
              </el-time-select>
              <el-time-select placeholder="结束时间" format='HH:mm' value-format='HH:mm' v-model="form.goOffWorkTime2"
                v-if="open" :picker-options="{
                  start: '00:10',
                  step: '00:10',
                  end: '23:50',
                  minTime: form.isNextTime2 == 1 ? '00:00' : form.goToWorkTime2
                }">
              </el-time-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="次日打卡" prop="isNextTime2">
              <el-radio-group v-model="form.isNextTime2">
                <el-radio :label="0">否</el-radio>
                <el-radio :label="1">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="16">
            <el-form-item label="第三次上下班时间" prop="goToWorkTime3">
              <el-time-select placeholder="起始时间" format='HH:mm' value-format='HH:mm' v-model="form.goToWorkTime3"
                v-if="open" :picker-options="{
                  start: '00:10',
                  step: '00:10',
                  end: '23:50'
                }">
              </el-time-select>
              <el-time-select placeholder="结束时间" format='HH:mm' value-format='HH:mm' v-model="form.goOffWorkTime3"
                v-if="open" :picker-options="{
                  start: '00:10',
                  step: '00:10',
                  end: '23:50',
                  minTime: form.isNextTime3 == 1 ? '00:00' : form.goToWorkTime3
                }">
              </el-time-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="次日打卡" prop="isNextTime3">
              <el-radio-group v-model="form.isNextTime3">
                <el-radio :label="0">否</el-radio>
                <el-radio :label="1">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listClasses, getClasses, delClasses, addClasses, updateClasses } from "@/api/cld/classes";

export default {
  name: "Classes",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 班次管理表格数据
      classesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classesName: null,
      },
      // 表单参数
      form: {
        // 是否特殊班-默认否0
        isSpecial: 0,
        isNextTime1: 0,
        isNextTime2: 0,
        isNextTime3: 0,
      },
      // 表单校验
      rules: {
        classesName: [
          { required: true, message: "请输入班次名称", trigger: "blur" },
        ],
        goToWorkTime1: [
          { required: true, message: "请输入第一次上下班时间", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询班次管理列表 */
    getList() {
      this.loading = false;
      listClasses(this.queryParams).then(response => {
        this.classesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        classesId: null,
        classesName: null,
        goToWorkTime1: null,
        goOffWorkTime1: null,
        goToWorkTime2: null,
        goOffWorkTime2: null,
        goToWorkTime3: null,
        goOffWorkTime3: null,
        isSpecial: 0,
        isNextTime1: 0,
        isNextTime2: 0,
        isNextTime3: 0,
      };
      // this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      console.log(this.form)
      this.title = "添加班次管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const classesId = row.classesId || this.ids
      getClasses(classesId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改班次管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.goOffWorkTime1 || this.form.goToWorkTime1) {
            if (!this.form.goOffWorkTime1 || !this.form.goToWorkTime1) {
              this.$message.error("第一个班次上下班时间必须都存在！");
              return;
            }
          }
          if (this.form.goOffWorkTime2 || this.form.goToWorkTime2) {
            if (!this.form.goOffWorkTime2 || !this.form.goToWorkTime2) {
              this.$message.error("第二个班次上下班时间必须都存在！");
              return;
            }
          }
          if (this.form.goOffWorkTime3 || this.form.goToWorkTime3) {
            if (!this.form.goOffWorkTime3 || !this.form.goToWorkTime3) {
              this.$message.error("第三个班次上下班时间必须都存在！");
              return;
            }
          }
          if (this.form.classesId != null) {
            updateClasses(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClasses(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const classesIds = row.classesId || this.ids;
      this.$modal.confirm('是否确认删除班次管理编号为"' + classesIds + '"的数据项？').then(function () {
        return delClasses(classesIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
  }
};
</script>
<style scoped>
.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 49%;
  margin-right: 1%;
}
</style>
